package com.kqb.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kqb.common.constant.UserType;
import com.kqb.common.curd.impl.BaseServiceImpl;
import com.kqb.common.domain.AjaxResult;
import com.kqb.common.domain.ErrorResult;
import com.kqb.common.domain.page.PageResult;
import com.kqb.common.domain.pojo.User;
import com.kqb.mapper.DepartmentMapper;
import com.kqb.mapper.EemployeeMapper;
import com.kqb.mapper.AdminUserMapper;
import com.kqb.pojo.Department;
import com.kqb.pojo.Employee;
import com.kqb.pojo.dto.EmployeeDto;
import com.kqb.pojo.dto.EmployeeQueryDto;
import com.kqb.pojo.vo.EmployeeVo;
import com.kqb.service.IEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 杉果树皮
 * @date 2025/2/17 20:11
 * @description:
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee, EemployeeMapper> implements IEmployeeService {

    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public AjaxResult save(EmployeeDto entity) {
        User user = new User(entity.getAccount(), entity.getPassword(), (byte) UserType.SYS_USER);
        if (adminUserMapper.selectByName(user.getAccount()) != 0) {
            return AjaxResult.error("员工已存在，无法再次添加");
        }
        int re = adminUserMapper.save(user);
        ErrorResult.business(re, "员工", "员工添加失败");
        Employee employee = new Employee();
        BeanUtils.copyProperties(entity, employee);
        employee.setId(user.getId());
        int res = iBaseMapper.save(employee);
        ErrorResult.business(res, "员工", "员工添加失败");

        return AjaxResult.success("员工添加成功");
    }

    @Override
    @Transactional
    public AjaxResult deleteById(Integer id) {
        int re = adminUserMapper.delete(id);
        ErrorResult.business(re, "员工模块", "删除员工失败");
        int res = iBaseMapper.delete(id);
        ErrorResult.business(res, "员工模块", "删除员工失败");

        return AjaxResult.success("删除成功");
    }

    @Override

    public AjaxResult selectByPage(EmployeeQueryDto employeeQueryDTO) {
        if (!"".equals(employeeQueryDTO.getDepartment())) {
            Department department = departmentMapper.selectByName(employeeQueryDTO.getDepartment());
            employeeQueryDTO.setDepartmentId(department.getId());
        }
        PageHelper.startPage(employeeQueryDTO.getPageNum(), employeeQueryDTO.getPageSize());
        List<EmployeeVo> employeeVos = iBaseMapper.queryByPage(employeeQueryDTO);
        PageInfo<EmployeeVo> employeeVOPageInfo = new PageInfo<>(employeeVos);

        return AjaxResult.success(new PageResult(employeeQueryDTO.getPageNum(), employeeQueryDTO.getPageSize(), employeeVOPageInfo.getTotal(), employeeVOPageInfo.getList()));

    }

    @Override
    public AjaxResult selectByType(String type) {
        return AjaxResult.success("查询销售成功", iBaseMapper.selectByType(type));
    }
}
